/**
 * 
 */
package com.zxc.redis.RReadWriteLock;

import java.util.concurrent.TimeUnit;

import org.redisson.Redisson;
import org.redisson.api.RReadWriteLock;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;

/**
 * @author Administrator
 *
 */
public class RReadWriteLockDemo {

	public static void main(String[] args) throws InterruptedException {
		Config config = new Config();
		config
			//.setCodec(new FstCodec())
			.useSingleServer() //单例模式
			.setAddress("192.168.1.122:6379")//连接地址
			.setDatabase(1) // 数据库索引位置
			.setPassword("shijie99test") // 密码
			.setClientName("channelbatch") // 客户端名称
			;
		RedissonClient redisson = Redisson.create(config);
		
		RReadWriteLock rwlock = redisson.getReadWriteLock("anyRWLock");
		
		// Most familiar locking method
		rwlock.readLock().lock();
		// or
		rwlock.writeLock().lock();
		
		// Lock time-to-live support
		// releases lock automatically after 10 seconds
		// if unlock method not invoked
		rwlock.readLock().lock(10, TimeUnit.SECONDS);
		// or
		rwlock.writeLock().lock(10, TimeUnit.SECONDS);
		
		// Wait for 100 seconds and automatically unlock it after 10 seconds
		boolean res = rwlock.readLock().tryLock(100, 10, TimeUnit.SECONDS);
		// or
		res = rwlock.writeLock().tryLock(100, 10, TimeUnit.SECONDS);
		
		
		rwlock.readLock().unlock();
		// or
		rwlock.writeLock().unlock();
		
		redisson.shutdown();
	}
}
